package com.example.agricultural_big_data.service.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.example.agricultural_big_data.mapper.VerificationCodeMapper;
import com.example.agricultural_big_data.pojo.Result;
import com.example.agricultural_big_data.pojo.ResultCode;
import com.example.agricultural_big_data.pojo.User;
import com.example.agricultural_big_data.service.VerificationCodeService;
import com.example.agricultural_big_data.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xiao yang
 * @Description: 验证码
 * @date 2021/3/913:28
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    VerificationCodeMapper verificationCodeMapper;

    @Autowired
    Result result;

    @Override
    public JSONObject verficationCode(JSONObject data) {
        JSONObject jsonObject;
        String code=data.getString("verifyCode");
        String dbcode=(String)redisUtil.get(code);
        User user=verificationCodeMapper.getUserByName(data.getString("name"));
        if (user==null){
            jsonObject=result.responseResult(ResultCode.fail.getStatusCode(),ResultCode.fail.getMsg());
        }else if (dbcode!=null){
            jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg());
        }else {
            jsonObject=result.responseResult(ResultCode.no_match.getStatusCode(),ResultCode.no_match.getMsg());
        }
        return jsonObject;
    }
}
